package com.chinacache.maint.workorder.action;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.chinacache.maint.commons.entity.BaseEntity;
import com.chinacache.maint.commons.orm.hibernate.Page;
import com.chinacache.maint.commons.web.struts2.CRUDActionSupport;
import com.chinacache.maint.workorder.entity.Field;
import com.chinacache.maint.workorder.entity.FieldAttribute;
import com.chinacache.maint.workorder.entity.FieldClass;
import com.chinacache.maint.workorder.entity.FieldClassToField;
import com.chinacache.maint.workorder.entity.Flow;
import com.chinacache.maint.workorder.entity.FlowFieldClass;
import com.chinacache.maint.workorder.service.FieldAttributeManager;
import com.chinacache.maint.workorder.service.FieldClassManager;
import com.chinacache.maint.workorder.service.FlowClassManager;
import com.chinacache.maint.workorder.service.FlowFieldClassManager;
import com.chinacache.maint.workorder.service.FlowManager;

@SuppressWarnings("serial")
public class FlowAction extends CRUDActionSupport<Flow> {
	private FlowManager flowManager;
	private FlowClassManager flowClassManager;
	private FlowFieldClassManager flowFieldClassManager;
	private FieldClassManager fieldClassManager;
	private FieldAttributeManager fieldAttributeManager;
	private Flow entity;
	private Integer id;
	private Page<Flow> page = new Page<Flow>();
	private Page<FieldClass> pageFieldClass = new Page<FieldClass>();
	private Integer flowClassId;
	private Integer[] fcId;
	private Integer ffcId;
	private Integer flowId;
	Map<List, Map> view = null;

	public Map<List, Map> getView() {
		return view;
	}

	public void setView(Map<List, Map> view) {
		this.view = view;
	}

	public void setFlowId(Integer flowId) {
		this.flowId = flowId;
	}

	public void setFfcId(Integer ffcId) {
		this.ffcId = ffcId;
	}

	@Override
	public String delete() throws Exception {
		if(entity.getFlowFieldClasss().size()>0)
		{
			this.msg="该子流程存在表单，请先删除表单！";
			return list();
		}
		if(entity.getWorkOrderSteps().size()>0)
		{
			this.msg="该流程已经被应用，不允许删除！";
			return list();
		}
		try {
			flowManager.deleteFlowByAction(id, flowClassId);
		} catch (Exception e) {
			this.msg = "系统异常，请与管理员联系！";
			e.printStackTrace();
		}
		return list();
	}

	public String above() throws Exception {
		if (id != null) {
			Flow flow = flowManager.getFlow(id);
			if (flow.getStatus() != BaseEntity.FLOW_STATUS_START) {
				Flow updateFlow = flowManager
						.getFlowByOrderAndFlowClassId(flow.getOrder()
								.intValue() - 1, flow.getFlowClass().getId());
				int flowOrder = flow.getOrder();
				int flowStatus = flow.getStatus();
				int updateFlowOrder = updateFlow.getOrder();
				int updateFlowStatus = updateFlow.getStatus();
				updateFlow.setStatus(flowStatus);
				updateFlow.setOrder(flowOrder);
				flow.setOrder(updateFlowOrder);
				flow.setStatus(updateFlowStatus);
				flowManager.updateFlow(updateFlow);
				flowManager.updateFlow(flow);
			}
		}
		return list();
	}

	public String down() throws Exception {
		Flow flow = flowManager.getFlow(id);
		if (flow.getStatus() != BaseEntity.FLOW_STATUS_END) {
			Flow updateFlow = flowManager.getFlowByOrderAndFlowClassId(flow
					.getOrder().intValue() + 1, flow.getFlowClass().getId());
			int flowOrder = flow.getOrder();
			int flowStatus = flow.getStatus();
			int updateFlowOrder = updateFlow.getOrder();
			int updateFlowStatus = updateFlow.getStatus();
			updateFlow.setStatus(flowStatus);
			updateFlow.setOrder(flowOrder);
			flow.setOrder(updateFlowOrder);
			flow.setStatus(updateFlowStatus);
			flowManager.updateFlow(updateFlow);
			flowManager.updateFlow(flow);
		}
		return list();
	}

	public String view() {

		view = new LinkedHashMap<List, Map>();
		for (FlowFieldClass ffc : entity.getFlowFieldClasss()) {
			Map<String, List> fas = new LinkedHashMap<String, List>();
			FieldClass fieldClass = ffc.getFieldClass();

			for (FieldClassToField fctField : fieldClass.getFieldClassFields()) {
				List<List> fasList = new ArrayList<List>();

				Field field = fctField.getField();
				Set<FieldAttribute> faSet = field.getFieldAttibutes();
				List<Integer> groupIds = new ArrayList<Integer>();
				for (FieldAttribute fa : faSet) {
					if (!groupIds.contains(fa.getGroupId())) {
						groupIds.add(fa.getGroupId());
					}
				}
				for (Integer groupId : groupIds) {
					List faByGroupIdList = fieldAttributeManager
							.getFieldAttributeByGroupId(groupId);
					fasList.add(faByGroupIdList);

				}
				fas.put(field.getName(), fasList);

			}
			List<String> aList = new ArrayList<String>();
			aList.add(fieldClass.getName());
			aList.add(fieldClass.getMemo());
			view.put(aList, fas);

		}
		return "view";
	}

	public String audit() throws Exception { 
		entity.setAudit(BaseEntity.AUDIT_YES);
		flowManager.updateFlow(entity);

		return list();
	}

	public String update() {
		return "update";
	}

	public String doUpdate() throws Exception {
		flowManager.saveFlow(entity);
		return list();
	}

	public String fieldClass() throws Exception {
		return "fieldClass";
	}

	public String showFieldClass() throws Exception {
		pageFieldClass = fieldClassManager.getAllFieldClass(pageFieldClass,null);
		return "showFieldClass";
	}

	public String addFieldClass() throws Exception {
		for (int i = 0; i < fcId.length; i++) {
			FlowFieldClass flowFieldClass = new FlowFieldClass();
			FieldClass fieldClass = fieldClassManager.getFieldClass(fcId[i]);
			flowFieldClass.setFieldClass(fieldClass);
			flowFieldClass.setFlow(entity);
			flowFieldClassManager.saveFlowFieldClass(flowFieldClass);
		}
		return fieldClass();
	}

	public String deleteFfc() throws Exception {
		flowFieldClassManager.deleteFlowFieldClass(ffcId);
		return fieldClass();
	}

	@Override
	public Flow getModel() {
		return entity;
	}

	@Override
	public Page<Flow> getPage() {
		return page;
	}

	@Override
	public String list() throws Exception {
		page = flowManager.getAllFlowByFlowClassId(page, flowClassId);
		return SUCCESS;
	}

	@Override
	public void prepare() throws Exception {
		if (id == null && flowId != null)
			id = flowId;
		if (id != null) {
			flowId = id;
			entity = flowManager.getFlow(id);
		} else {
			entity = new Flow();
		}
	}

	@Override
	public String save() throws Exception {
		page = flowManager.getAllFlowByFlowClassId(page, flowClassId);
		entity.setFlowClass(flowClassManager.getFlowClass(flowClassId));
		if (page.getList().size() == 0)
			entity.setStatus(BaseEntity.FLOW_STATUS_START);
		else
			entity.setStatus(BaseEntity.FLOW_STATUS_END);
		if (page.getList().size() == 0)
			entity.setOrder(1);
		else
			entity.setOrder(new Integer(page.getList().get(
					page.getList().size() - 1).getOrder().intValue() + 1));
		entity.setAudit(BaseEntity.AUDIT_NO);
		flowManager.saveFlow(entity);
		Flow updateFlow = flowManager.getFlowByOrderAndFlowClassId(entity
				.getOrder().intValue() - 1, entity.getFlowClass().getId());
		if (page.getList().size() > 1 && updateFlow != null) {
			updateFlow.setStatus(BaseEntity.FLOW_STATUS_PRO);
			flowManager.updateFlow(updateFlow);
		}
		return list();
		// return RELOAD;
	}

	public String next() throws Exception {
		page = flowManager.getAllFlowByFlowClassId(page, flowClassId);
		if (page.getList().size() == 0) {
			entity.setStatus(BaseEntity.FLOW_STATUS_START);
			entity.setOrder(new Integer(1));
		} else {
			entity.setStatus(BaseEntity.FLOW_STATUS_PRO);
			entity.setOrder(new Integer(page.getList().get(
					page.getList().size() - 1).getOrder().intValue() + 1));
		}
		entity.setFlowClass(flowClassManager.getFlowClass(flowClassId));
		entity.setAudit(BaseEntity.AUDIT_NO);
		flowManager.saveFlow(entity);
		Flow updateFlow = flowManager.getFlowByOrderAndFlowClassId(entity
				.getOrder().intValue() - 1, entity.getFlowClass().getId());
		if (page.getList().size() > 1 && updateFlow != null) {
			updateFlow.setStatus(BaseEntity.FLOW_STATUS_PRO);
			flowManager.updateFlow(updateFlow);
		}
		return "input";
	}

	public void setFlowManager(FlowManager flowManager) {
		this.flowManager = flowManager;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getFlowClassId() {
		return flowClassId;
	}

	public void setFlowClassId(Integer flowClassId) {
		this.flowClassId = flowClassId;
	}

	public void setFlowClassManager(FlowClassManager flowClassManager) {
		this.flowClassManager = flowClassManager;
	}

	public Page<FieldClass> getPageFieldClass() {
		return pageFieldClass;
	}

	public Flow getEntity() {
		return entity;
	}

	public void setFlowFieldClassManager(
			FlowFieldClassManager flowFieldClassManager) {
		this.flowFieldClassManager = flowFieldClassManager;
	}

	public void setFcId(Integer[] fcId) {
		this.fcId = fcId;
	}

	public void setFieldClassManager(FieldClassManager fieldClassManager) {
		this.fieldClassManager = fieldClassManager;
	}

	public Integer getFlowId() {
		return flowId;
	}

	public void setFieldAttributeManager(
			FieldAttributeManager fieldAttributeManager) {
		this.fieldAttributeManager = fieldAttributeManager;
	}
}
